/*   The MIT License
 *   
 *   Carina Engine
 *   Copyright (c) 2009 2010 2011 2012 Zdravko Velinov
 *   
 *   Permission is hereby granted, free of charge, to any person obtaining a copy
 *   of this software and associated documentation files (the "Software"), to deal
 *   in the Software without restriction, including without limitation the rights
 *   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *   copies of the Software, and to permit persons to whom the Software is
 *   furnished to do so, subject to the following conditions:
 *
 *   The above copyright notice and this permission notice shall be included in
 *   all copies or substantial portions of the Software.
 *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *   THE SOFTWARE.
 */
#include "carina/node-manager.hh"

namespace Carina
{
ModelNodeFactory::ModelNodeFactory(const WorldManagerPtr& wmgr)
    :   m_WorldManager(wmgr)
{
}

NodePtr ModelNodeFactory::create(const string& creation_data)
{
    ModelPtr model = m_WorldManager->getModel(creation_data);
    ModelInstancePtr model_inst = make_aligned_shared<ModelInstance>(model);
    ModelNodePtr model_node = make_aligned_shared<ModelNode>(m_WorldManager, model_inst);
    return model_node;
}

NodePtr TransformNodeFactory::create(const string& creation_data)
{
    return make_aligned_shared<TransformNode>();
}

LightSourceNodeFactory::LightSourceNodeFactory(const LightingManagerPtr& lmgr)
    :   m_LightingManager(lmgr)
{
}

NodePtr LightSourceNodeFactory::create(const string& creation_data)
{
    LightSourcePtr lsource = m_LightingManager->create(creation_data);
    if(lsource->isGlobalLightSource())
        THROW_EXCEPTION("Invalid light source: " + creation_data);
    return make_aligned_shared<LightSourceNode>(static_pointer_cast<SpatialLightSource>(lsource));
}

NodeManager::NodeManager()
{
}

NodeManager::NodeManager(const WorldManagerPtr& wmgr, const LightingManagerPtr& lmgr)
{
    init(wmgr, lmgr);
}

void NodeManager::init(const WorldManagerPtr& wmgr, const LightingManagerPtr& lmgr)
{
    registerFactory("Transform node", make_aligned_shared<TransformNodeFactory>());
    registerFactory("Model node", make_aligned_shared<ModelNodeFactory>(wmgr));
    registerFactory("Light source node", make_aligned_shared<LightSourceNodeFactory>(lmgr));
}

NodePtr NodeManager::create(const string& name, const string& creation_data)
{
    iterator iter = getFactory(name);
    if(iter == getFactoryEnd())
        THROW_EXCEPTION("Unknown factory: " + name);
    return iter->second->create(creation_data);
}
}